from time import strftime
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from common.helper import log, dirname


class Base:
    def __init__(self, driver):
        self.driver = driver

    # 加入显示等待并定位元素
    def find_element(self, *loc):
        try:
            WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc))
        except:
            log(str(loc)+'没有找到')
        else:
            return self.driver.find_element(*loc)

    # 加入显示等待并定位元素组
    def find_elements(self, *loc):
        try:
            WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc))
        except:
            log(str(loc)+'没有找到')
        else:
            return self.driver.find_elements(*loc)

    # 截图
    def save_image(self, image_name):
        picture = dirname(strftime('%Y%y%d:%H%M%S') + image_name, 'image')
        self.driver.get_screenshot_as_file(picture)

    # 执行js语句
    def execute_script(self, script):
        return self.driver.execute_script(script)